﻿using System;
using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;
using System.IO;
using AMS.DeDuplicator.CommonLayer;
using AMS.DeDuplicator.CommonLayer.Adapter;

namespace AMS.DeDuplicator.ApplicationLayer
{
    public partial class DeDuplicatorForm : Form
    {
        FormOperations _operations;

        public DeDuplicatorForm()
        {
            InitializeComponent();
            _operations = new FormOperations(pbProgress, lblProgress, new TreeViewAdapter(tvResults));
        }

        private void HandleBtnBrowseClick(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtRootDirectory.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void HandleBtnFindDuplicatesClick(object sender, EventArgs e)
        {
            // validate directory
            if (txtRootDirectory.Text == "")
                return;

            try
            {
                _operations.FindDuplicates(txtRootDirectory.Text, new TreeViewAdapter(tvResults), cbRecurse.Checked);

                if (tvResults.Nodes.Count > 0)
                    btnDelete.Enabled = true;
            }
            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("Invalid Directory!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void HandleBtnDeleteClick(object sender, EventArgs e)
        {
            // delete/replace selected duplicates
            TreeViewAdapter adapter = new TreeViewAdapter(tvResults);
            _operations.DeleteDuplicates(adapter, cbReplace.Checked);

            // update TreeView (remember this for undo, also)
            HandleBtnFindDuplicatesClick(null, null);
        }

        private void HandleNodeDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            _operations.OpenFile(e.Node.Text);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            _operations.ClearTemp();
            base.OnClosing(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            _operations.ProcessKeyDown(e.KeyCode);
            base.OnKeyDown(e);
        }
    }
}
